<template>
  <el-dialog
    title="提示"
    :visible.sync="dialogVisible"
    width="30%"
    @close="closeDialog"
  >
    <el-form ref="formDialog" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="form.description" type="textarea" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch v-model="form.enVisible" :active-value="1" :inactive-value="0" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="queryDialog">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { putPermission, addPermission, getPermission } from '@/api/permission'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    type: {
      type: Array,
      default: null
    }
  },
  data() {
    return {
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: 0
      },
      rules: {
        name: [{ required: true, message: 'name不能为空', trigger: 'blur' }],
        code: [{ required: true, message: 'code不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    closeDialog() {
      this.$emit('update:dialog-visible', false)
      this.$refs.formDialog.resetFields()
      this.form.description = ''
    },
    queryDialog() {
      this.$refs.formDialog.validate(async(ok) => {
        if (ok) {
          if (this.type[0] === null) {
            await putPermission(this.form)
            this.$message.success('修改成功')
          } else {
            await addPermission({ ...this.form, type: this.type[0], pid: this.type[1] })
            this.$message.success('添加成功')
          }

          this.closeDialog()
          this.$emit('remove')
        }
      })
    },
    async singleData() {
      this.form = await getPermission(this.type[1])
    }
  }
}

</script>

<style lang="scss" scoped>

</style>
